<?php
$DEVELOPMENT=1;
$LOG_FILE = '/tmp/primaKlimaAlarm.log';

// DEVELOPMENT
if ($DEVELOPMENT == 1) {
	$LOG_FILE = 'primaKlimaAlarm.log';
}

$tempSystem = $_REQUEST ['system'];
$tempStatus = $_REQUEST ['status'];

// letzten status ermitteln
$tempLog = file ( $LOG_FILE );
foreach ( $tempLog as $tempRow ) {
	list ( $tempTime, $tempLogSystem, $tempLogStatus, $dummy ) = explode ( ',', $tempRow );
	$tempStatusHolder[$tempLogSystem] = $tempLogStatus;
}

// reagieren
if (array_key_exists ( $tempSystem, $tempStatusHolder )) {
	if ($tempStatusHolder[$tempSystem] != $tempStatus) {
		// log schreiben
		$tempFile = fopen ( $LOG_FILE, "a" ) or die ( "Unable to open log file!" );
		$tempLogMessage = date ( "H:i:s" ) . ',' . $tempSystem . ',' . $tempStatus . ",d\n";
		fwrite ( $tempFile, $tempLogMessage );
		fclose ( $tempFile );
		
		// mail senden
		$tempMessage = "System " . $tempSystem . " steht auf Status " . ($tempStatus?"an":"aus");
		$temmpResult = mail('vl5@vl5.de', 'ALARM NOTIFIER', $tempMessage);
	}
}
?>